package com.group7d;

import com.group7d.entity.TestResult;
import com.group7d.fileparser.JtlFileParser;
import com.group7d.repository.DBUtils;
import com.group7d.util.SystemConfig;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 程序启动入口.
 *
 * @author jinshulin (jinshulin170531@credithc.com)
 * @version 0.0.1-SNAPSHOT
 * @since 2018年05月02日 22时09分
 */
public class Main {

    public static void main(String[] args) throws InterruptedException {
        if (args.length < 2) {
            System.out.println("usage: java -jar dbConfigFile JtlFileParser.jar xxx.jtl");
            System.exit(-1);
        }
        SystemConfig.initSystemConfig(args[0]);
        String jtlFilePath = args[1];
        loadJtlFile(jtlFilePath);
    }

    private static void loadJtlFile(String jtlFilePath) {
        List<TestResult> resultList = null;
        try {
            resultList = JtlFileParser.readFile(new File(jtlFilePath));
        }
        catch (IOException e) {
            e.printStackTrace();
            System.exit(-2);
        }
        int i = 0;
        for (i = 0; i < resultList.size(); i += 500) {
            DBUtils.saveAll(resultList.subList(i, i + 500));
        }
        int leftSize = i % 500;
        if (leftSize > 0) {
            int size = resultList.size();
            DBUtils.saveAll(resultList.subList(size - leftSize, size));
        }
    }
}
